# -*- coding: utf-8 -*-

'''
Created on 09-07-10

@author: peterd
'''




from web.service.LBCParser import LBCParser
from geo.models import NoSuchCity, NoSuchStreet, NoSuchLocation
from infra.logging import logger


class SlspParser(LBCParser):
	'''
	classdocs
	'''

	def __init__(self, lbcategory, dataProcessor): 
		LBCParser.__init__(self, lbcategory, dataProcessor)

		self.inAtm_ = False
		self.inUrl_ = False
		self.url_ = None
		
						
		log = logger.getLogger()
		log.debug("%s = %s" % (self, self.__dict__))
		
		
	def start_td(self, attrs):
		if len(attrs) == 0:			
			self.inAtm_ = True
		else:
			self.inAtm_ = False
			
		
	
	def end_td(self):
		if self.inAtm_:
			self.inAtm_ = False
					
						
	def start_a(self, attrs):
		if self.inAtm_:
			for attr, value in attrs:
				if attr == 'href':
					self.inUrl_ = True
					self.url_ = self.dataProcessor_.getUrl(value)					
					break
				
					
	def end_a(self):
		self.inUrl_ = False
				
	 
	def do_handle_data(self, data):
		log = logger.getLogger()
	
		#REVISIT: 'td>' is not properly handled, gets through as data	
		if self.inAtm_ and not self.inUrl_ and data != 'td>':
			try:
				self.setCity(data)
				self.setStreet(data)
				self.createOrganization(name = u'Slovenská Sporitelňa, a.s.', \
								            	url = self.url_, \
								            	phone = 'Sporotel: 0850 111 888', \
												logo = 'icons/lbc/atm/slsp.png', \
												email = '')
			except NoSuchCity, e:
				log.error('NoSuchCity exception caught, reason: %s' % e)
			except NoSuchStreet, e:
				log.error('NoSuchStreet exception caught, reason: %s' % e)
			except NoSuchLocation, e:
				log.error('NoSuchLocation exception caught, reason: %s' % e)
			except Exception, e:
				log.error('An exception caught, reason: %s' % e)

			self.reset__()

	def getId(self):
		return self.__class__.__name__